#include <fcntl.h>
#include <termios.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>


int main(void)
{
    int fds[2];
    int rc, maxfd, i;
    
    char buff[7];
    fd_set  inset1,inset2;
    struct timeval tv;
    if((fds[0] = open("hello1", O_RDWR|O_CREAT, 0666)) < 0)
    {
      perror("open hello1 error ");
      return -1;
    }
    if((fds[1] = open("hello2", O_RDWR|O_CREAT, 0666)) < 0)
    {
      perror("open hello2 error ");
      return -1;
    }
    if((rc = write(fds[0], "Hello!\n", 7)))
      printf("rc=%d\n", rc);
    
    lseek(fds[0], 0, SEEK_SET);

    maxfd = fds[0]>fds[1] ? fds[0] : fds[1];

    FD_ZERO(&inset1);
    FD_SET(fds[0], &inset1);

    FD_ZERO(&inset2);
    FD_SET(fds[1], &inset2);
    tv.tv_sec = 2;
    tv.tv_usec = 0;

    while(FD_ISSET(fds[0], &inset1) || FD_ISSET(fds[1], &inset2))
    {
        if(select(maxfd+1, &inset1, &inset2, NULL, &tv) < 0) 
          perror("select error");
        else
        {
            if(FD_ISSET(fds[0], &inset1)) 
            {
                rc = read(fds[0], buff, 7);
                if(rc > 0)
                {
                    buff[rc] = '\0';
                    printf("read: %s", buff);
                }
                else
                  perror("read error");
            }
            if(FD_ISSET(fds[1], &inset2)) 
            {
                rc = write(fds[1], buff, 7);
                if(rc > 0)
                {
                    buff[rc] = '\0';
                    printf("rc=%d,write: %s", rc, buff);
                }
                else
                  perror("write error");
                sleep(2);
            }
        }
    }
    exit (0);
}

